//
//  NSString+Extension.swift
//  SwiftForAllKnow
//
//  Created by enghou on 16/9/30.
//  Copyright © 2016年 xyxorigation. All rights reserved.
//
import UIKit
extension String{
    func StringSize(font : UIFont) -> CGSize {
        let str = NSString.init(string: self)
        let attribute = [NSAttributedStringKey.font:font]
        let size = str.size(withAttributes: attribute)
        return size;
    }
    
    func stringSize(font : UIFont,maxWid : CGFloat)->CGSize{
        let str = NSString.init(string: self)
        let attribute = [NSAttributedStringKey.font : font]
        let maxSize = CGSize.init(width: maxWid, height: CGFloat(MAXFLOAT))
        let size = str.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: attribute, context: nil).size
        return size
    }
    
    func md5() -> String {
        let str = self.cString(using: String.Encoding.utf8)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(str!, strLen, result)
        let hash = NSMutableString()
        for i in 0 ..< digestLen {
            hash.appendFormat("%02x", result[i])
        }
        result.deinitialize()
        return String(format: hash as String)
    }
    
}
